﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: frmRegisterClass.cs
#endregion

using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.finan.entries
{
	/// <summary>
	/// Description of frmRegisterGroup.
	/// </summary>
	public partial class frmRegisterSubClass : csharp.common.RegisterTemplate
	{
		private SubClass subclass;
		private SubClasss listSubClass;
		private Classs listClass;
		private SubGroups listSubgroups;
		private Groups    listGroups;
		
		private bool mutexGroup;
		private bool mutexSubgroup;
		
		public frmRegisterSubClass()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			subclass = new SubClass();
			listSubClass = new SubClasss();
			listClass  = new Classs();
			listSubgroups  = new SubGroups();
			listGroups     = new Groups();
		}
		
		protected override void newEvent()
		{
			subclass.clear();
			txtCode.Focus();
		}		
		
		/// <summary>
		/// Set data from edits to class
		/// </summary>
		protected override void setClass()
		{
			subclass.code= txtCode.Text;
			subclass.name= txtGroup.Text;
			subclass.active= cbxActive.Checked;
			subclass.classId= (cbxClass.SelectedItem as Class).id;//listClass.get(cbxClass.SelectedIndex).id;
		}
		
		/// <summary>
		/// Set data from class to edits
		/// </summary>
		protected override void setEdits()
		{
			subclass.id= dgvMain.CurrentRow.Cells["id"].Value.ToString();
			
			if (subclass.get())
			{
				txtCode.Text= subclass.code;
				txtGroup.Text= subclass.name;
				cbxActive.Checked= subclass.active;
				
				//listClass.search(subclass.classId);
				//cbxClass.SelectedIndex = listClass.getIndex();
				Functions.setSelectIndexComboClass(cbxClass, subclass.classId);
				
				//listSubgroups.search(listClass.get().subGroupId);
				//cbxSubGroup.SelectedIndex= listSubgroups.getIndex();
				
				//listGroups.search(listSubgroups.get().groupId);
				//cbxGroup.SelectedIndex= listGroups.getIndex();
			}
		}
		
		/// <summary>
		/// Fill the DataGridView
		/// </summary>
		protected override void fillDataGridView()
		{
			int tam;
			
			//listSubClass.fillList();
			dgvMain.Rows.Clear();
			tam= listSubClass.count();
			for (int i=0; i < tam; i++)
			{
				String []row= {listSubClass.get(i).id, listSubClass.get(i).code, listSubClass.get(i).name};
				dgvMain.Rows.Add(row);
			}
		}
		
		/// <summary>
		/// Find the object of search
		/// </summary>
		protected override void find()
		{
			common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
		}		
		
	   /// <summary>
	   /// Save the register in the database.
	   /// </summary>
	   /// <returns>bool - true if the register was saved sucessful, false case not.</returns>	
	   protected override bool save()
	   {
	   	if(subclass.save())
	   	{
				listSubClass.fillList((cbxClass.SelectedItem as Class).id);
				fillDataGridView();   		
	   		return true;
	   	}
	   	return false;		   		
	   }
	   
	   /// <summary>
	   /// Update the register in the database
	   /// </summary>
	   /// <returns>bool - true if the register was updated sucessful, false case not.</returns>
	   protected override bool update()
	   {   
	   	if (subclass.update())
	   	{
				listSubClass.fillList((cbxClass.SelectedItem as Class).id);
				fillDataGridView();   		
	   		return true;
	   	}
	   	return false;		   		
	   }
	   
	   /// <summary>
	   /// Delete the register in the database.
	   /// </summary>
	   /// <returns>bool - true if the register was deleted sucessful, false case not.</returns>
	   protected override bool delete()
	   {
	   	subclass.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
	   	if (subclass.delete())
	   	{
				listSubClass.fillList((cbxClass.SelectedItem as Class).id);
				//fillDataGridView();   		
	   		return true;
	   	}
	   	return false;	   		
	   }
	   
	   /// <summary>
	   /// Method call in initialization
	   /// </summary>
	   protected override void init()
	   {
	   	KryptonDataGridViewTextBoxColumn id   = new KryptonDataGridViewTextBoxColumn();
	   	KryptonDataGridViewTextBoxColumn code = new KryptonDataGridViewTextBoxColumn();
	   	KryptonDataGridViewTextBoxColumn name = new KryptonDataGridViewTextBoxColumn();
	   	
	   	dgvMain.Columns.AddRange(new DataGridViewColumn[] {id,code,name});
	   	
	   	id.Name= "id";
	   	id.Visible= false;
	   	
	   	code.Name = "code";
	   	code.HeaderText= "Código";
	   	
	   	name.Name= "name";
	   	name.Width= 200;
	   	name.HeaderText = "Nome";
	   	
	   	listSubClass.fillList();
	   	
	   	//Fill groups
	   	listGroups.fillList();
	   	Functions.fillComboGroups(listGroups, cbxGroup, "id", "name");
	   	//int tam= listGroups.count();
	   	//for (int i=0; i < tam; i++)
	   	//	cbxGroup.Items.Add(listGroups.get(i).name);
	   	
	   	//Fill subgroups
	   	listSubgroups.fillList();
	   	Functions.fillComboSubGroups(listSubgroups, cbxSubGroup, "id", "name");
	   	//tam= listSubgroups.count();
	   	//for (int i=0; i < tam; i++)
	   	//	cbxSubGroup.Items.Add(listSubgroups.get(i).name);
	   	
	   	//Fill classes
	   	listClass.fillList();
	   	Functions.fillComboClass(listClass, cbxClass, "id", "name");
	   	//tam= listClass.count();
	   	//for (int i=0; i < tam; i++)
	   	//	cbxClass.Items.Add(listClass.get(i).name);
	   }		
		
		private void TabPrincipalEnter(object sender, EventArgs e)
		{
			txtCode.Focus();
		}
		
		void CbxGroupSelectedValueChanged(object sender, EventArgs e)
		{
			if ((!mutexGroup) && (cbxGroup.SelectedIndex >= 0))
			{
				listSubgroups.fillList((cbxGroup.SelectedItem as Group).id);
				
				Functions.fillComboSubGroups(listSubgroups, cbxSubGroup, "id", "name");
			}
		}
		
		void CbxSubGroupSelectedValueChanged(object sender, EventArgs e)
		{
			if (cbxSubGroup.SelectedIndex >= 0)
			{
				if (!mutexSubgroup)
				{
					listClass.fillList((cbxSubGroup.SelectedItem as SubGroup).id);
					
					Functions.fillComboClass(listClass, cbxClass, "id", "name");
				}
				
				mutexGroup = true;
				Functions.setSelectIndexComboGroup(cbxGroup, (cbxSubGroup.SelectedItem as SubGroup).groupId);
				mutexGroup = false;				
			}
		}
		
		void CbxClassSelectedValueChanged(object sender, EventArgs e)
		{
			if (cbxClass.SelectedIndex >= 0)
			{
				mutexSubgroup = true;
				Functions.setSelectIndexComboSubGroup(cbxSubGroup, (cbxClass.SelectedItem as Class).subGroupId);
				mutexSubgroup = false;
				
				listSubClass.fillList((cbxClass.SelectedItem as Class).id);
				fillDataGridView();
				
				if (txtCode.Text.Equals(String.Empty))
				{
					int aux = listSubClass.count() + 1;
					
					if (aux < 10)
						txtCode.Text = "0"+aux;
					else
						txtCode.Text = aux.ToString();
				}
			}
		}
	}
}
